/*
 * Main.java
 *
 * Created on March 14, 2005, 1:50 PM
 */

package org.infoset.xsdoc;

import java.util.*;
import java.io.*;

import java.net.URL;
import java.util.logging.Level;
import java.util.logging.LogManager;
import org.infoset.xml.XMLException;
import org.infoset.xml.jaxp.SAXTransformerSourceFilter;
import org.infoset.xml.sax.SAXDocumentLoader;
import org.infoset.xml.util.WriterItemDestination;

/**
 *
 * @author R. Alexander Milowski
 */
public class Main
{
   
static String fineLog = 
"handlers= java.util.logging.ConsoleHandler\n"+
".level= FINE\n"+
"java.util.logging.ConsoleHandler.level = FINE\n"+
"java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter\n";
;
static String finerLog = 
"handlers= java.util.logging.ConsoleHandler\n"+
".level= FINER\n"+
"java.util.logging.ConsoleHandler.level = FINER\n"+
"java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter\n";
;
static String finestLog = 
"handlers= java.util.logging.ConsoleHandler\n"+
".level= FINEST\n"+
"java.util.logging.ConsoleHandler.level = FINEST\n"+
"java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter\n";
;
   public static void setLogLevel(Level level) {
      if (level==Level.FINE) {
         try {
            LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(fineLog.getBytes()));
         } catch (java.io.IOException ex) {
            ex.printStackTrace();
         }
      } else if (level==Level.FINER) {
         try {
            LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(finerLog.getBytes()));
         } catch (java.io.IOException ex) {
            ex.printStackTrace();
         }
      } else if (level==Level.FINEST) {
         try {
            LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(finestLog.getBytes()));
         } catch (java.io.IOException ex) {
            ex.printStackTrace();
         }
         
      }
   }
   /** Creates a new instance of Main */
   public Main()
   {
   }

   static void usage() {
      System.err.println("Usage: xsdoc {options} schema-document");
      System.err.println("  -t\t Transforms the output into XHTML documentation.");
      System.err.println("  -o file\t The file to which to send the output.");
   }
   /**
    * @param args the command line arguments
    */
   public static void main(String[] args)
   {
      boolean transform = false;
      File outputFile = null;
      int argidx = 0;
      File prefixConfig = null;
      while (argidx<args.length && args[argidx].charAt(0)=='-') {
         if (args[argidx].equals("-h")) {
            usage();
            System.exit(1);
         } else if (args[argidx].equals("-t")) {
            transform = true;
         } else if (args[argidx].equals("-o")) {
            argidx++;
            if (argidx>=args.length) {
               System.err.println("The -o option requires a file parameter.");
               System.exit(1);
            }
            outputFile = new File(args[argidx]);
         } else if (args[argidx].equals("-p")) {
            argidx++;
            if (argidx>=args.length) {
               System.err.println("The -p option requires a file parameter.");
               System.exit(1);
            }
            prefixConfig = new File(args[argidx]);
         } else if (args[argidx].equals("-l")) {
            argidx++;
            if (argidx>=args.length) {
               System.err.println("The -l option requires a file parameter.");
               System.exit(1);
            }
            String loglevel = args[argidx];
            if (loglevel.equals("fine")) {
               setLogLevel(Level.FINE);
            } else if (loglevel.equals("finer")) {
               setLogLevel(Level.FINER);
            } else if (loglevel.equals("finest")) {
               setLogLevel(Level.FINEST);
            }
         } else {
            System.err.println("Unknown option: "+args[argidx]);
            System.exit(1);
         }
         argidx++;
      }

      if ((args.length-argidx)<1) {
         usage();
         System.exit(1);
      }
      
      if (transform && outputFile==null) {
         System.err.println("An output file must be specified when transforming.");
         System.exit(1);
      }
      
      try {
         Expander expander = new Expander();
         expander.setDocumentLoader(new SAXDocumentLoader());

         for (int i=argidx; i<args.length; i++) {
            File f = new File(args[i]);
            expander.loadSchema(f.toURI());
         }
         
         Iterator errors = expander.getErrors();
         if (errors.hasNext()) {
            while (errors.hasNext()) {
               System.err.println(errors.next());
            }
         } else {
            Writer out = outputFile==null ? new OutputStreamWriter(System.out,"UTF-8") :
                                            new OutputStreamWriter(new FileOutputStream(outputFile),"UTF-8");
            if (transform) {
               SAXTransformerSourceFilter xform = new SAXTransformerSourceFilter();
               URL location = Main.class.getResource("expanded2xhtml.xsl");
               xform.loadTransform(location);
               xform.attach(new WriterItemDestination(out,"UTF-8",false));

               expander.write(xform,null);
               
            } else {
               expander.write(new WriterItemDestination(out,"UTF-8",false),null);
            }
            if (outputFile!=null) {
               out.flush();
               out.close();
            }
            System.exit(0);
         }
      } catch (XMLException ex) {
         ex.printStackTrace();
      } catch (Exception ex) {
         ex.printStackTrace();
      }
      System.exit(1);
   }
   
}
